
/*
*    FileName: protocolconversion.h
* Description: convert protocols
*     Version: 0.1
*      Author: wulin
* Create Date: 2011-03,29
*       Input: QByteArray, ascii coded strings from application
*      OutPut: QByteArray, bit stream to be sent to WSN GateWay
    整个工程最好只实例化一次这个类，因为这个类会记录当前正在被控制的mac,以防止
    同时多个人控制同一个mac
*/
#ifndef PROTOCOLCONVERSION_H
#define PROTOCOLCONVERSION_H

#include <QObject>
#include <QStringList>
#include <QHash>
#include "protocal.h"
#include <QMap>
class QSqlQuery;
class DBOInThread;
struct StructCache
{
    //存储经常查询的内容，减少数据库访问
    QHash <QByteArray,qint64> hashSensorNameID;
    QHash <QByteArray,qint64> hashCmdStringEncode;
    QHash <QByteArray,qint64> hashCmdStringBytes;
    QHash <QByteArray,QByteArray> hashDataTypeParam;
    QHash <QByteArray,QByteArray> hashParamType;
    QHash <QByteArray,qint64> hashParamBytes;
    QHash <QByteArray,QByteArray> hashByteMacProtcHeader;

};

class ProtocolConversion : public QObject
{
    Q_OBJECT
public:
    explicit ProtocolConversion(QString DBConectionName,QObject *parent = 0);
    ~ProtocolConversion();

    QByteArray  convertionAppToGWStepOne(const QByteArray &originalData,QByteArray baHexAppMac,
           const QMap <QString, Protocal>  &mapHeadProtocal);
    QByteArray  convertionGWToAppStepOne(const QByteArray &originalData,Protocal currentProtocol);

    QStringList controllingMacList; //记录正在被控制的mac地址
    QByteArray baLastSNBase10; //10进制的SN

signals:

public slots:

private:

    /************************************************
      following functions are to be called by
      convertionAppToGWStepOne(QByteArray &Orignal)
      *********************************************/
    QByteArray  getHeader(QByteArray nodeMacAddr); //search database,return such as "aa55"
    QByteArray  getTYPE(const QByteArray &typeString); //such as "02"
    QByteArray  getDestAddr(const QByteArray &destNodeAddr); //such as IP for the macAddr
    QByteArray  getSourceAddr(QByteArray baHexAppMac);//
    QByteArray  getSN(const QByteArray &SNByteArr); //maybe register it in table aliveCmdRec
    //QByteArray  getLen();
    QByteArray  getPktType(const QByteArray &PktTypeStr);
    QByteArray  getReserved();
    QByteArray  getData( QList<QByteArray> &list);

    void    updateControllingMacList(QString strMacAddr,bool add); //更新变量controllingMacList

    DBOInThread *parentDBOInThread;
    Protocal currentWSNProtocol; //当前正在用于转换的网关接口协议
    StructCache myCache; //存储一些数据库的内容
    QString dboConName;

};

#endif // PROTOCOLCONVERSION_H
